// subpages1/allStoreList/allStoreList.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    i:1,//分页
    storeList:null,//商家列表
    goodsShow:null,
    keyword: '',
    isShow:true,
    distance:null,
    imgUrl: app.globalData.domin,
    ModalShow:false,
    a:0,
    total:0
  },
  onLoad() {
    let _this = this
    wx.getLocation({//获取用户定位
      success:(res)=>{
        let {latitude,longitude} =res
        let distance = [latitude,longitude]
        wx.setStorageSync('distance', distance)
        _this.setData({
          distance
        })
        wx.setStorageSync('a',-1)
        _this.getshopIndex()
      },
      complete() {
        wx.setStorageSync('a',0)
      }
    })
    _this.getshopIndex()
  },
  onShow: function () {
    let distance=wx.getStorageSync('distance')
    let a=wx.getStorageSync('a')
    a++
    console.log('a',a)
    console.log('distance',distance)
    if(a>=1) {
      if(distance=='') {
        setTimeout(() => {
          this.setData({
            ModalShow:true
          })
        }, 3000);
      }else if(distance!=='') {
        this.getshopIndex()
        wx.setStorageSync('a',a)
      }else {
        wx.setStorageSync('a',a)
      }
    }
    
  },
  closeinfo() {
    this.setData({
      ModalShow:false
    })
    wx.setStorageSync('a',0)
  },
  openSetting(e) {//跳转授权设置之后的回调
    let _this = this
    
    if (e.detail.authSetting['scope.userLocation']) { //此处同上同理
      wx.getLocation({
        type: 'wgs84',
        success(res) {
          let {latitude,longitude} =res
          let distance = [latitude,longitude]
          wx.setStorageSync('distance', distance)
          wx.setStorageSync('a',-1)
          let a=wx.getStorageSync('a')
          console.log('a1',a)
          _this.setData({
            distance,
            ModalShow:false
          })
          _this.getshopIndex()
        },
        complete(err) {
          console.log('定位数据',err)
        }
      })
    }
  },
  onHide() {
    console.log('后台')
    wx.setStorageSync('a',-1)
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      i:1
    })
    this.getshopIndex()
    setTimeout(()=>{
      wx.stopPullDownRefresh()
    }, 1000)
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let _this = this
    let { i , keyword ,distance ,storeList ,total} = _this.data
    i++
    let data = {
      page:i,
      size:10,
      map:'applet_merchant_shop_list',
      distance,
    }
    // if(keyword!=='') {
      data.keyword = keyword
    // }
    if(distance==null) {
      data.distance = ''
    }
    if(storeList.length<total) {
      wx.request({
        url: app.globalData.requestUrl,
        data: data,
        success: function(res) {
          let list = res.data.data.list
          if (res.data.ec == 200) {
            if(list.length!==0) {
              list.map(val=>{
                storeList.push(val)
              })
              _this.setData({
                storeList,
                goodsShow:true,
                i
              })
            }else {
              _this.setData({
                goodsShow:false
              })
            }
          }
        }
      });
    }else {
      _this.setData({
        goodsShow:false
      })
    }
  },
  onShareAppMessage(){

  },
  //商家列表
  getshopIndex() {
    let _this = this
    let { i , keyword ,distance} = _this.data
    let data = {
      page:i,
      size:10,
      map:'applet_merchant_shop_list',
      distance
    }
    // if(keyword!=='') {
      data.keyword = keyword
    // }
    if(distance==null) {
      data.distance = ''
    }
    wx.request({
      url: app.globalData.requestUrl,
      data: data,
      success: function(res) {
        console.log('商家列表xila',res.data.data.pages);
        if (res.data.ec == 200) {
          console.log('商家列表xila',res.data)
          _this.setData({
            storeList: res.data.data.list,
            isShow:true
          })
          if(res.data.data.count) {
            console.log('res.data.data.count',res.data.data.count)
            _this.setData({
              total:res.data.data.count
            })
          }
          if(res.data.data.total){
            console.log('res.data.data.total',res.data.data.total)
            _this.setData({
              total:res.data.data.total
            })
          }
        } else {
          _this.setData({
            isShow:false
          })
        }
      }
    });
  },
  //门店详情
  tostoredetails(e) {
    wx.navigateTo({
      url: '/subpages1/merchantInfo/merchantInfo?mid='+e.currentTarget.dataset.id
    })
  },
  //搜索
  searchFor() {
    let keyword = this.data.keyword
    this.setData({
      i:1
    })
    this.getshopIndex()

  },
  bindKeyInput(e) {//搜索input
    this.setData({
      keyword: e.detail.value
    })
  },
})